# coding=utf8
from app.extensions import db
from app.modules.common.utils import ComplexEncoder
from app.modules.common.dbUtil import createId
from sqlalchemy import func


class ProjectPlanInfo(db.Model, ComplexEncoder):
    __tablename__ = "project_plan"
    plan_id = db.Column(db.String, primary_key = True)
    plan_name = db.Column(db.String)
    plan_code = db.Column(db.String)
    expect_integration_date = db.Column(db.Date)  # 预计集成日期
    expect_release_date = db.Column(db.Date)  # 预计上线日期
    plan_status = db.Column(db.String)
    zt_id = db.Column(db.String)
    start_date = db.Column(db.Date)
    end_date = db.Column(db.Date)
    members = None
    PM = db.Column(db.String)
    PM_name = db.Column(db.String)
    isRemove = db.Column(db.String)
    create_date = db.Column(db.DateTime, default = func.now())
    isEmergency = db.Column(db.String)
    bug_num = db.Column(db.String)
    need_num = db.Column(db.String)
    test_head = db.Column(db.String)
    test_head_name = db.Column(db.String)
    error_desc = db.Column(db.String)
    link_product = db.Column(db.String)
    last_build_time = db.Column(db.Date)
    isDisable = None
    projects = None
    notCreateProCount = None

    def __init__(self, plan_name = None, plan_code = None, expect_integration_date = None, expect_release_date = None,
                 plan_status = None, zt_id = None, start_date = None, end_date = None, members = None, PM = None,
                 PM_name = None, isDisable = None, isRemove = '1', isEmergency = '1', bug_num = None, need_num = None,
                 test_head = None, test_head_name = None, error_desc = None, link_product = None,
                 last_build_time = None):
        self.plan_id = createId("pp")
        self.plan_name = plan_name
        self.plan_code = plan_code
        self.expect_integration_date = expect_integration_date
        self.expect_release_date = expect_release_date
        self.plan_status = plan_status
        self.zt_id = zt_id
        self.start_date = start_date
        self.end_date = end_date
        self.members = members
        self.PM = PM
        self.PM_name = PM_name
        self.isDisable = isDisable
        self.isRemove = isRemove
        self.isEmergency = isEmergency
        self.bug_num = bug_num
        self.need_num = need_num
        self.test_head = test_head
        self.test_head_name = test_head_name
        self.error_desc = error_desc
        self.link_product = link_product
        self.last_build_time = last_build_time

    def to_dict(self):
        return {
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'plan_code': self.plan_code,
            'expect_integration_date': self.expect_integration_date.strftime(
                '%Y-%m-%d') if self.expect_integration_date else None,
            'expect_release_date': self.expect_release_date.strftime(
                '%Y-%m-%d') if self.expect_release_date else None,
            'start_date': self.start_date.strftime('%Y-%m-%d') if self.start_date else None,
            'end_date': self.end_date.strftime('%Y-%m-%d') if self.end_date else None,
            'plan_status': self.plan_status,
            'zt_id': self.zt_id,
            'members': self.members,
            'PM': self.PM,
            'PM_name': self.PM_name,
            'isDisable': self.isDisable,
            'isRemove': self.isRemove,
            'isEmergency': self.isEmergency,
            'bug_num': self.bug_num,
            'need_num': self.need_num,
            'test_head': self.test_head,
            'test_head_name': self.test_head_name,
            'error_desc': self.error_desc,
            'link_product': self.link_product,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'last_build_time': self.last_build_time.strftime('%Y-%m-%d') if self.last_build_time else None,
        }


class PlanMember(db.Model):
    __tablename__ = "plan_member"
    plan_id = db.Column(db.String, primary_key = True)
    plan_name = None
    account = db.Column(db.String, primary_key = True)
    realname = db.Column(db.String)
    role = db.Column(db.String)

    def __init__(self, plan_id = None, account = None, realname = None, role = None):
        self.plan_id = plan_id
        self.account = account
        self.realname = realname
        self.role = role

    def to_dict(self):
        return {
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'account': self.account,
            'realname': self.realname,
            'role': self.role,
        }
